<?php

namespace app\model\user;

use app\base\BaseModel;
use app\model\order\PayLogModel;
use think\facade\Cache;
use think\facade\Db;

/**
 * 模型-UserMemberOrderModel
 * Class UserMemberOrderModel
 * @package app\model
 */
class UserMemberOrderModel extends BaseModel
{

    protected $name = 'user_member_order';

    /**
     * 充值余额
     * @param $level_id
     * @param $user_id
     * @return array
     * @throws \Psr\SimpleCache\InvalidArgumentException
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getMemberPayInfo($level_id, $user_id)
    {
        $order_id = $this->addOrder($level_id, $user_id);
        $pay_info = [];
        if ($order_id !== false) {
            $pay_info = $this->getOrderPayInfo($order_id, $user_id);
        }
        return $pay_info;
    }

    /**
     * 添加充值订单
     * @param $level_id
     * @param $user_id
     * @return bool|int
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function addOrder($level_id, $user_id)
    {
        $map = [
            'level_id' => $level_id,
            'status' => 0,
            'user_id' => $user_id
        ];
        $this->where($map)->delete();
        $user_level_model = new UserLevelModel();
        $user_level = $user_level_model->getInfo($level_id, 'level_price');
        $order_sn = date("YmdHis") . $user_id . mt_rand(1000, 9999);
        $in_data = [
            'order_sn' => $order_sn,
            'user_id' => $user_id,
            'level_id' => $level_id,
            'price' => $user_level['level_price'],
            'add_time' => time(),
            'add_date' => date("Y-m-d H:i:s")
        ];
        $ret = $this->insert($in_data);
        if ($ret != false) {
            $order_id = $this->getLastInsID();
            return $order_id;
        }
        return false;
    }

    /**
     * 获取充值订单支付信息
     * @param $order_id
     * @param $user_id
     * @param string $pay_mode
     * @return array
     * @throws \Psr\SimpleCache\InvalidArgumentException
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getOrderPayInfo($order_id, $user_id, $pay_mode = 'weixin')
    {
        $pay_sn = Cache::store('redis')->get($this->redis_key . 'member' . $order_id, 0);
        if (empty($pay_sn)) {
            $pay_sn = "member" . date("YmdHis") . $order_id . rand(1000, 9999);
            Cache::store('redis')->set($this->redis_key . 'member' . $order_id, $pay_sn, 600);
        }
        $map = [
            'id' => $order_id,
            'user_id' => $user_id,
            'status' => 0
        ];
        $order_data = $this->where($map)->find();
        if (empty($order_data)) {
            throw new \think\Exception("支付信息不存在！", 0);
        }
        $order_id = $order_data['id'];
        $order_sn = $order_data['order_sn'];
        Db::startTrans();
        $order_total = $order_data['price'];
        $up_pay = [
            'pay_sn' => $pay_sn,
        ];
        $ret = $this->where($map)->update($up_pay);
        $out_data = [
            'order_id' => $order_id,
            'order_sn' => $order_sn,
            'pay_sn' => $pay_sn,
            'total' => $order_total
        ];
        if ($ret !== false) {
            PayLogModel::addLog($out_data, "member", $pay_mode);
            Db::commit();
        } else {
            Db::rollback();
            return [];
        }
        return $out_data;
    }

    /**
     * 完成支付订单
     * @param $pay_sn
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function endOrderPay($pay_sn)
    {
        $map = [
            'pay_sn' => $pay_sn
        ];
        $member = $this->where($map)->field('user_id,level_id')->find();
        $up_pay = [
            'status' => 1
        ];
        $ret = $this->where($map)->update($up_pay);
        if ($ret !== false) {
            $user_model = new UserModel();
            $ret = $user_model->updateMemberLevel($member['user_id'], $member['level_id']);
            if ($ret !== false) {
                return true;
            }
        }
        return false;
    }


}
